<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('题库列表')" />
</head>
<body class="gray-bg">
     <div class="container-div">
        <div class="row">
            <div class="col-sm-12 search-collapse">
                <form id="formId">
                    <div class="select-list">
                        <ul>
                            <li>
                                <label>题库：</label>
                                <input type="text" name="poolName"/>
                            </li>
                            <li>
                                Vip题库：<select name="status" th:with="type=${@dict.getType('sys_user_vip')}">
                                <option value="">所有</option>
                                <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                            </select>
                            </li>
                            <li>
                                题库展示：<select name="status" th:with="type=${@dict.getType('pool_show')}">
                                <option value="">所有</option>
                                <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                            </select>
                            </li>
                            <li shiro:hasRole="admin">
                                <label>创建者：</label>
                                <input type="text" name="createBy"/>
                            </li>
                            <li shiro:hasAnyRoles="admin">
                                <label>创建时间：</label>
                                <input type="text" class="time-input" placeholder="请选择创建时间" name="createTime"/>
                            </li>
                            <li shiro:hasRole="admin">
                                <label>更新者：</label>
                                <input type="text" name="updateBy"/>
                            </li>
                            <li shiro:hasRole="admin">
                                <label>更新时间：</label>
                                <input type="text" class="time-input" placeholder="请选择更新时间" name="updateTime"/>
                            </li>
                            <li>
                                <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
                                <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
                            </li>
                        </ul>
                    </div>
                </form>
            </div>

            <div class="btn-group-sm" id="toolbar" role="group">
                <a class="btn btn-success" onclick="$.operate.add()" shiro:hasPermission="system:pool:add">
                    <i class="fa fa-plus"></i> 添加
                </a>
                <a class="btn btn-primary single disabled" onclick="$.operate.edit()" shiro:hasPermission="system:pool:edit">
                    <i class="fa fa-edit"></i> 修改
                </a>
                <a class="btn btn-danger multiple disabled" onclick="$.operate.removeAll()" shiro:hasPermission="system:pool:remove">
                    <i class="fa fa-remove"></i> 删除
                </a>
                <a class="btn btn-info" onclick="$.table.importExcel()" shiro:hasPermission="system:pool:import">
                    <i class="fa fa-upload"></i> 导入
                </a>
                <a class="btn btn-warning" onclick="exportExcel($('form').attr('id'),'')" shiro:hasPermission="system:pool:export">
                    <i class="fa fa-download"></i> 导出
                </a>
            </div>
            <div class="col-sm-12 select-table table-striped">
                <table id="bootstrap-table"></table>
            </div>
        </div>
    </div>
    <th:block th:include="include :: footer" />
    <script th:inline="javascript">
        var editFlag = [[${@permission.hasPermi('system:pool:edit')}]];
        var removeFlag = [[${@permission.hasPermi('system:pool:remove')}]];
        var adminRoleFlag = [[${@permission.hasRole('admin')}]];
        var exportFlag = [[${@permission.hasPermi('system:pool:export')}]];
        var prefix = ctx + "system/pool";

        $(function() {
            var options = {
                importTemplateUrl: prefix + "/importTemplate",
                url: prefix + "/list",
                createUrl: prefix + "/add",
                updateUrl: prefix + "/edit/{id}",
                removeUrl: prefix + "/remove",
                importUrl: prefix + "/importData",
                exportUrl: prefix + "/export",
                modalName: "题库",
                columns: [{
                    checkbox: true
                },
                {
                    field: 'poolId',
                    title: '题库id',
                    visible: false
                },
                {
                    field: 'poolName',
                    title: '题库名称',
                    align: "center"
                },
                {
                    field: 'isVip',
                    title: 'Vip题库',
                    align: "center"
                },
                {
                    visible: editFlag == 'hidden' ? false : true,
                    title: '展示状态',
                    align: 'center',
                    formatter: function (value, row, index) {
                        return statusTools(row);
                    }
                },
                {
                    field: 'createBy',
                    title: '创建者'
                },
                {
                    field: 'createTime',
                    title: '创建时间',
                    align: "center"
                },
                {
                    field: 'updateBy',
                    title: '更新者',
                    align: "center"
                },
                {
                    field: 'updateTime',
                    title: '更新时间',
                    align: "center"
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function(value, row, index) {
                        var actions = [];
                        actions.push('<a class="btn btn-success btn-xs ' + editFlag +  adminRoleFlag + '" href="javascript:void(0)" onclick="$.operate.edit(\'' + row.poolId + '\')"><i class="fa fa-edit"></i>编辑</a> ');
                        actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + adminRoleFlag +'" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.poolId + '\')"><i class="fa fa-remove" ></i>删除</a> ');
                        actions.push('<a class="btn btn-warning btn-xs ' + exportFlag + '" href="javascript:void(0)" onClick="exportExcel(\'' + row.poolId + '\', \'' + row.poolName + '\')"><i class="fa fa-download"></i>导出</a>');
                        return actions.join('');
                    }
                }]
            };
            $.table.init(options);
        });
        // 导出数据
        function exportExcel(poolId, poolName) {
            debugger
            var row = $("#" + table.options.id).bootstrapTreeTable('getSelections')[0];
            table.set();
            $.modal.confirm("确定导出" + table.options.modalName + "吗？", function() {
                var currentId = $.common.isEmpty(poolId) ? $('form').attr('id') : poolId;
                var params = $("#" + table.options.id).bootstrapTable('getOptions');
                var dataParam = $("#" + currentId).serializeArray();
                dataParam.push({ "name": "poolId", "value": poolId})
                dataParam.push({ "name": "poolName", "value": poolName})
                dataParam.push({ "name": "orderByColumn", "value": params.sortName });
                dataParam.push({ "name": "isAsc", "value": params.sortOrder });
                $.modal.loading("正在导出数据，请稍候...");
                $.post(table.options.exportUrl, dataParam, function(result) {
                    if (result.code == web_status.SUCCESS) {
                        window.location.href = ctx + "common/download?fileName=" + encodeURI(result.msg) + "&delete=" + true;
                    } else if (result.code == web_status.WARNING) {
                        $.modal.alertWarning(result.msg)
                    } else {
                        $.modal.alertError(result.msg);
                    }
                    $.modal.closeLoading();
                });
            });
        }
        /* 展示状态显示 */
        function statusTools(row) {
            if (row.isShow == 1) {
                return '<i class=\"fa fa-toggle-off text-info fa-2x\" onclick="enable(\'' + row.poolId +'\', \''+ row.poolName + '\')"></i> ';
            } else {
                return '<i class=\"fa fa-toggle-on text-info fa-2x\" onclick="disable(\'' + row.poolId +'\', \''+ row.poolName + '\')"></i> ';
            }
        }

        /* 题库管理-停止展示 */
        function disable(poolId, poolName) {
            $.modal.confirm("确认要停止展示该题库吗？", function() {
                $.operate.post(prefix + "/changeStatus", { "poolId": poolId, "poolName": poolName, "isShow": 1 });
            })
        }

        /* 题库管理-展示 */
        function enable(poolId, poolName) {
            $.modal.confirm("确定展示该题库吗？", function() {
                $.operate.post(prefix + "/changeStatus", { "poolId": poolId, "poolName": poolName, "isShow": 0 });
            })
        }
    </script>
</body>
<!-- 导入区域 -->
<script id="importTpl" type="text/template">
    <form enctype="multipart/form-data" class="mt20 mb10">
        <div class="col-xs-offset-1">
            <input type="file" id="file" name="file"/>
            <div class="mt10 pt5">
                &nbsp;	<a onclick="$.table.importTemplate()" class="btn btn-default btn-xs"><i class="fa fa-file-excel-o"></i> 下载模板</a>
            </div>
            <font color="red" class="pull-left mt10">
                提示：仅允许导入“xls”或“xlsx”格式文件！
            </font>
        </div>
    </form>
</script>
</html>